package com.guigu.qingcheng.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.guigu.qingcheng.mapper.TbAdMapper;
import com.guigu.qingcheng.pojo.TbAd;
import com.guigu.qingcheng.service.TbAdService;
import com.guigu.qingcheng.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;


/**
*
* @author Jiang
* @date 2020/08/05
*/
@Service
public class TbAdServiceImpl implements TbAdService {
    @Autowired(required = false)
    private TbAdMapper tbAdMapper;



    @Override
    public List<TbAd> queryAll() {
        List<TbAd> tbAd = tbAdMapper.selectAll();
            return tbAd;
    }

    @Override
    public PageUtil<TbAd> findListAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<TbAd> pages = (Page<TbAd>) tbAdMapper.selectAll();
        PageUtil<TbAd> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
    }


    @Override
    public PageUtil<TbAd> pageQueryByAll(Integer pageNum, Integer pageSize, TbAd tbAd) {

        Example example=new Example(TbAd.class);
        Example.Criteria criteria = example.createCriteria();
        // TODO 待实现 重新实现判断参数

        if(!StringUtils.isEmpty(tbAd.getName())){
            criteria.andLike("name",'%'+tbAd.getName()+'%');
        }
        if(!StringUtils.isEmpty(tbAd.getPosition())){
           criteria.andEqualTo("position",tbAd.getPosition());
        }
        if(!StringUtils.isEmpty(tbAd.getEndTime())){
            criteria.andGreaterThanOrEqualTo("endTime",tbAd.getEndTime());
            LocalDate date = tbAd.getEndTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate().plusDays(1);
            criteria.andLessThan("endTime",date);
        }
        PageHelper.startPage(pageNum,pageSize);
        Page<TbAd> pages = (Page<TbAd>) tbAdMapper.selectByExample(example);
        PageUtil<TbAd> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
     }


    @Override
    public boolean saveTbAd(TbAd tbAd) {
        tbAd.setSeq(0);
        int insert = tbAdMapper.insert(tbAd);
        if(insert>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateTbAd(TbAd tbAd) {
        int i = tbAdMapper.updateByPrimaryKeySelective(tbAd);
        if(i>0){
            return true;
        }
        return false;
    }


    @Override
    public boolean deleteById(Integer id) {
        int i = tbAdMapper.deleteByPrimaryKey(id);
        if(i>0){
            return true;
        }
        return false;
    }

    @Override
    public TbAd findById(Integer id) {
        TbAd tbAd = tbAdMapper.selectByPrimaryKey(id);
        return tbAd;
    }

    /**
     * 根据广告位置查询广告列表
     * @param position
     * @return
     */
    @Override
    public List<TbAd> findByPosition(String position) {
        Example example=new Example(TbAd.class);
        example.setOrderByClause("seq desc");
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("position",position);//位置
        criteria.andLessThanOrEqualTo("startTime",new Date());
        //开始时间小于当前时间
        criteria.andGreaterThanOrEqualTo("endTime",new Date());
        //截至时间大于当前时间
        criteria.andEqualTo("status","1");//状态有效
        return tbAdMapper.selectByExample(example);
    }

    @Override
    public boolean updateSeq(TbAd tbAd) {
        Integer seq = tbAdMapper.selectMaxSeq(tbAd.getPosition());
        tbAd.setSeq(seq+1);
        int i = tbAdMapper.updateByPrimaryKeySelective(tbAd);
        if(i>0){
            return true;
        }
        return false;
    }
}
